import {
    getEmployeeList,

} from "@/apis/common.js";

import {
    orderCancel,
    getRepairView,
    orderHit,
    orderAssToNew,
    orderAss
} from "@/apis/workOrder.js";

import {
    deepClone
} from "@/util";

export default {
    data() {
        return {
            editId: '',
            showSalesman: false,
            serviceList: [{}, {}, {}, {}, {}],
            currEmployee: '',
            employeeParams: {
                PageSize: 10,
                PageNo: 1,
                DeptId: '',
                GroupId: '',
                EmpName: '',
            },
            employeeList: [],

            orderForm: {
                Descr: '', // 下单记录
                AppointmentDate: '',
                AppointmentTime: '',
                SalerCharge: '',
                RewardAmount: '',
                TGID: '',
                VirtCharge1: '',
                VirtCharge2: '',
                VirtCharge3: '',
                VirtCharge4: '',
                VirtCharge5: '',
                Employee1: '',
                Employee2: '',
                Employee3: '',
                Employee4: '',
                Employee5: '',
                CustId: '',
                CustName: '',
                Zone1: '',
                Zone2: '',
                Zone3: '',
                Tel: '', //电话
                Zone5: '',
                Contact: '', //联系人
                Zone1_2: '',
                Zone2_2: '',
                Zone3_2: '',
                Tel2: '',
                Zone5_2: '', //详细地址
                Contact2: '',
                CallPhotos: [],
                Remark1: '',
                Remark2: '',
                Remark3: '',
                Remark4: '',
                Remark5: '',
                ItemId: '',
                ItemName: '',
                ItemStyle: '',
                ItemBrand: '',
                ItemCatg: '',
                SerialNo: '',
                BuyDate: '',
                ExpaireDate: '',
                OrderSrc: '',
                OrderNo: '',        // 来源单号
                CustType: '',       // 客户类型
                RepairType: '',     //  服务类型
                Saler: '',          // 业务员
                Priority: false,
                RepairCatg: '',     // 服务分类
                lnkRegion: '',      //区域
            },
        };
    },
    // onPageScroll(res) {
    // 	console.log('onPageScroll', res)

    // },
    onLoad(option) {
        console.log('option', option)
        if (option.id) {
            this.editId = option.id
            this.fetchRepairView()
        }
        this.fetchEmployeeList()
    },
    methods: {
        async fetchRepairView() {
            const custTypeMap = {
                0: "",
                1: '公司',
                2: '家庭'
            }
            const res = await getRepairView({
                RepairId: this.editId
            })
            if (res.MsgCode != 0) return
            console.log('fetchRepairView------->', res)
            const orderForm = res.data
            orderForm.CustType = custTypeMap[res.data.CustType]
            orderForm.Descr = res.data.CallDescr
            orderForm.Expense01 = parseFloat(orderForm.Expense01 ? orderForm.Expense01 : 0)
            orderForm.Expense02 = parseFloat(orderForm.Expense02 ? orderForm.Expense02 : 0)
            orderForm.Expense03 = parseFloat(orderForm.Expense03 ? orderForm.Expense03 : 0)
            orderForm.Expense04 = parseFloat(orderForm.Expense04 ? orderForm.Expense04 : 0)
            orderForm.Expense05 = parseFloat(orderForm.Expense05 ? orderForm.Expense05 : 0)
            orderForm.Revenue01 = parseFloat(orderForm.Revenue01 ? orderForm.Revenue01 : 0)
            orderForm.Revenue02 = parseFloat(orderForm.Revenue02 ? orderForm.Revenue02 : 0)
            orderForm.Revenue03 = parseFloat(orderForm.Revenue03 ? orderForm.Revenue03 : 0)
            orderForm.Revenue04 = parseFloat(orderForm.Revenue04 ? orderForm.Revenue04 : 0)
            orderForm.Revenue05 = parseFloat(orderForm.Revenue05 ? orderForm.Revenue05 : 0)
            orderForm.VirtCharge1 = parseFloat(orderForm.VirtCharge1 ? orderForm.VirtCharge1: 0)
            orderForm.VirtCharge2 = parseFloat(orderForm.VirtCharge2 ? orderForm.VirtCharge2: 0)
            orderForm.VirtCharge3 = parseFloat(orderForm.VirtCharge3 ? orderForm.VirtCharge3: 0)
            orderForm.VirtCharge4 = parseFloat(orderForm.VirtCharge4 ? orderForm.VirtCharge4: 0)
            orderForm.VirtCharge5 = parseFloat(orderForm.VirtCharge5 ? orderForm.VirtCharge5: 0)
            orderForm.SalerId = orderForm.Saler
            orderForm.Saler = orderForm.SalerName
            orderForm.Employee1 = orderForm.Engineer1Name
            orderForm.Employee2 = orderForm.Enginer2Name
            orderForm.Employee3 = orderForm.Enginer3Name
            orderForm.Employee4 = orderForm.Enginer4Name
            orderForm.Employee5 = orderForm.Enginer5Name

            delete orderForm.RepairDone
            delete orderForm.CreditDetail
            orderForm.CallPhotos = res.data.CallPhotos == '' ? [] : res.data.CallPhotos.split(',')
            this.imageList = orderForm.CallPhotos.map(item => {
                return { url: 'http://img.help10000.com' + item }
            })
            this.appointment = orderForm.AppointmentTime
            orderForm.lnkRegion = ''
            this.orderForm = orderForm

        },
        async handleOrderAdd() {
            const orderForm = deepClone(this.orderForm)
            const reqObj = {}
            reqObj.Employee1 = orderForm.Employee1 === orderForm.Engineer1Name && orderForm.Employee1 ? orderForm.Engineer1 : this.employeeList.find(o => o.EmpName === orderForm.Employee1)?.EmpId || ''
            reqObj.Employee2 = orderForm.Employee2 === orderForm.Enginer2Name && orderForm.Employee2 ? orderForm.Engineer2 : this.employeeList.find(o => o.EmpName === orderForm.Employee2)?.EmpId || ''
            reqObj.Employee3 = orderForm.Employee3 === orderForm.Enginer3Name && orderForm.Employee3 ? orderForm.Engineer3 : this.employeeList.find(o => o.EmpName === orderForm.Employee3)?.EmpId || ''
            reqObj.Employee4 = orderForm.Employee4 === orderForm.Enginer4Name && orderForm.Employee4 ? orderForm.Engineer4 : this.employeeList.find(o => o.EmpName === orderForm.Employee4)?.EmpId || ''
            reqObj.Employee5 = orderForm.Employee5 === orderForm.Enginer5Name && orderForm.Employee5 ? orderForm.Engineer5 : this.employeeList.find(o => o.EmpName === orderForm.Employee5)?.EmpId || ''
            reqObj.VirtCharge1 = orderForm.VirtCharge1
            reqObj.VirtCharge2 = orderForm.VirtCharge2
            reqObj.VirtCharge3 = orderForm.VirtCharge3
            reqObj.VirtCharge4 = orderForm.VirtCharge4
            reqObj.VirtCharge5 = orderForm.VirtCharge5
            reqObj.RepairIds = orderForm.ID
            console.log('handleOrderAdd>>>>>>>>>>>>>>>>>>',reqObj,orderForm )

            const res = await orderAss(reqObj)
            console.log('orderCancel res', res)

            if (res.MsgCode != 0) return
            uni.showToast({
                title: '派单成功',
                icon: "none",
                mask: true,
                position: "center",
            });
            setTimeout(() => {
                this.$navigateBack(this.$router, 1)
            }, 1000)

        },
        async handleOrderHit() {
           
            const res = await orderHit({
                RepairId: this.editId,
            })
            console.log('orderHit res', res)

            if (res.MsgCode != 0) return
            uni.showToast({
                title: '催单已下发',
                icon: "success",
                mask: true,
                position: "center",
            });

        },
        async handleAssToNew() {
           
            const res = await orderAssToNew({
                RepairId: this.editId,
            })
            if (res.MsgCode != 0) return
            uni.showToast({
                title: '已转成待安排',
                icon: "success",
                mask: true,
                position: "center",
            });
            setTimeout(() => {
                this.$navigateBack(this.$router, 1)
            }, 1000)
        },
        chargeSummation() {
            const form = this.orderForm
            form.Expense01 = (form.VirtCharge1 + form.VirtCharge2 + form.VirtCharge3 + form.VirtCharge4 + form.VirtCharge5).toFixed(2)
            this.orderForm = form
        },
        handleServicePick(key) {
            this.currEmployee = key
            this.showSalesman = true
        },
        handleEmployeeSearch() {
            this.employeeParams.PageNo = 1
            this.employeeList = []
            this.fetchEmployeeList()
        },
        scrolltoEmployeeLower() {
            this.employeeParams.PageNo = this.employeeParams.PageNo + 1
            this.fetchEmployeeList()

        },
        async fetchEmployeeList() {
            const res = await getEmployeeList(this.employeeParams)
            if (res.MsgCode != 0) return
            console.log('fetchEmployeeList', res)

            this.employeeList = [...this.employeeList, ...res.data.EmployeeList]
        },
    }
}
